# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=pycparser
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.18
pkgrel=1
pkgdesc='Complete parser of the C language, written in pure Python (mingw-w64)'
arch=('any')
url='https://github.com/eliben/pycparser'
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-ply"
             "${MINGW_PACKAGE_PREFIX}-python2-ply"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
#_dtoken="be/64/1bb257ffb17d01f4a38d7ce686809a736837ad4371bcc5c42ba7a715c3ac"
source=("${_realname}-${pkgver}.tar.gz::https://github.com/eliben/pycparser/archive/release_v${pkgver}.tar.gz")
sha256sums=('d5ead0f43ac5a8dd89e8475ada557037bbeb7ed709491861e84356ef17a3f8ac')

prepare() {
  cd "${srcdir}"
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-release_v${pkgver}" "python${pver}-build-${CARCH}"
  done
}

build() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
    cd pycparser
    ${MINGW_PREFIX}/bin/python${pver} _build_tables.py
  done  
}

package_python3-pycparser() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-ply")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-pycparser() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-ply")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pycparser() {
  package_python2-pycparser
}

package_mingw-w64-i686-python3-pycparser() {
  package_python3-pycparser
}

package_mingw-w64-x86_64-python2-pycparser() {
  package_python2-pycparser
}

package_mingw-w64-x86_64-python3-pycparser() {
  package_python3-pycparser
}
